package ifi.card.poker;

import java.util.HashMap;
import java.util.HashSet;

import ifi.card.Funtion.RegularFuntion;
import Definir.DefinirPokey;
import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Poker implements DefinirPokey, Comparable<Poker> {
	public float possitionXOrigine;
	public float possitionYOrigine;
	public float possitionX;
	public float possitionY;
	public int matier;
	public int numPoker;
	public boolean isSelect;
	RegularFuntion fonction = new RegularFuntion();

	public Poker() {
		possitionX = 0;
		possitionY = 0;

		possitionXOrigine = 0;
		possitionYOrigine = 0;
		isSelect = false;
		// TODO Auto-generated constructor stub
	}
	

	public void drawPoker(Canvas canvas, Bitmap bitmapPocker,
			Bitmap bitmapMatier, Bitmap bitmapJQK, float x, float y, int num,
			int matier1) {
		int matier = 3 -matier1;
		int orderPoker[] = new int[15];
		Object pok = new Object();
		int numMap;
		float xMidi;
		float yFront;
		float yAfter;
		float xFront;
		float xAfter;
		int numRow = 1;
		int numRowMidi = 0;
		canvas.drawBitmap(bitmapPocker, x, y, null);

		xFront = (int) (x + MAGIN_POKER + (SIZE_MATIER_POKER / 2));
		xAfter = x + WITH_POKER - MAGIN_POKER - (SIZE_MATIER_POKER / 2);
		yFront = y + MAGIN_POKER + (SIZE_MATIER_POKER / 2);
		yAfter = y + HIGHT_POKER - MAGIN_POKER - (SIZE_MATIER_POKER / 2);
		xMidi = (xFront + xAfter) / 2;
		float distance;
		
		if(num==14) numMap = 1; else
			if(num == 15) numMap =2; else
				numMap = num;

		if (numMap == 1) {
			int X, Y;
			X = (bitmapPocker.getWidth() - bitmapMatier.getWidth() / 4) / 2 + 7;
			Y = (bitmapPocker.getHeight() - bitmapMatier.getHeight() / 2) / 2;
			drawMatier(canvas, bitmapMatier, x + X, y + Y, matier);
		} else if (numMap < 11) {
			if (numMap < 4) {
				numRow = numMap;
				numRowMidi = 0;
			}
			if (numMap >= 4) {
				numRow = numMap / 2;
				if (numRow > 4)
					numRow = 4;
				numRowMidi = numMap - numRow * 2;
			}
			// phan nay de ve 2 ben cua quan bai
			distance = (yAfter - yFront) / (numRow - 1);
			for (int i = 0; i < numRow; i++) {
				if (numMap >= 4) {
					drawMatier(canvas, bitmapMatier, xFront, yFront + distance
							* i, matier);
					drawMatier(canvas, bitmapMatier, xAfter, yFront + distance
							* i, matier);
				} else if (numMap < 4) {
					drawMatier(canvas, bitmapMatier, xMidi, yFront + distance
							* i, matier);
				}
			}
			// Phan nay de ve o giua
			if (numRowMidi == 1) {

				if (numMap == 7)
					drawMatier(canvas, bitmapMatier, xMidi, yFront + distance
							/ 2, matier);
				else
					drawMatier(canvas, bitmapMatier, xMidi,
							(yFront + yAfter) / 2, matier);
			} else if (numRowMidi == 2) {
				drawMatier(canvas, bitmapMatier, xMidi, yFront + distance / 2,
						matier);
				drawMatier(canvas, bitmapMatier, xMidi, yAfter - distance / 2,
						matier);
			}
		} else {
			drawJQK(canvas, bitmapJQK, x, y, numMap, matier);
		}
		drawNumberJoker(canvas, numMap, x, y, matier);
	}

	private void drawJQK(Canvas canvas, Bitmap bitmapJQK, float x, float y,
			int num, int matier) {
		int X = 0, Y = 0;
		switch (matier) {
		case CO_POKER:
			Y = 0;
			break;
		case PIC_POKER:
			Y = 80;
			break;
		case RO_POKER:
			Y = 160;
			break;
		case TEP_POKER:
			Y = 240;
			break;
		default:
			break;
		}

		switch (num) {
		case 11:
			X = 0;
			break;
		case 12:
			X = 60;
			break;
		case 13:
			X = 120;
			break;
		default:
			break;
		}
		fonction.drawPartImg(canvas, bitmapJQK, x, y, X, Y, 60, 80);
	}

	// Phan nay de ve chat cua quan bai
	private void drawMatier(Canvas canvas, Bitmap bitmapMatier, float x,
			float y, int matier) {
		RegularFuntion draw = new RegularFuntion();
		draw.drawPartImg(canvas, bitmapMatier, x - (SIZE_MATIER_POKER / 2), y
				- (SIZE_MATIER_POKER / 2), matier * SIZE_MATIER_POKER, 0,
				SIZE_MATIER_POKER, SIZE_MATIER_POKER);
	}

	// Ve so cua cac quan bai
	private void drawNumberJoker(Canvas canvas, int num, float xPo, float yPo,
			int matier) {
		Paint paint = new Paint();
		String stringNumPoker = null;
		paint.setTextSize(MAGIN_POKER);
		switch (matier) {
		case 3-PIC_POKER:
		case 3-TEP_POKER:
			paint.setColor(0xff000000);
			break;
		case 3-RO_POKER:
		case 3-CO_POKER:
			paint.setColor(0xffFF0000);
			break;
		default:
			break;
		}

		if (num == 1) {
			stringNumPoker = "A";
		} else if (num < 11) {
			stringNumPoker = String.valueOf(num);
		} else {
			switch (num) {
			case 11:
				stringNumPoker = "J";
				break;
			case 12:
				stringNumPoker = "Q";
				break;
			case 13:
				stringNumPoker = "K";
				break;
			default:
				break;
			}

		}
		paint.setAntiAlias(true);
		canvas.drawText(stringNumPoker, xPo + MAGIN_POKER / 2, yPo
				+ MAGIN_POKER, paint);
		canvas.drawText(stringNumPoker, xPo + 47, yPo + 72, paint);
	}
	
	@Override
	public int compareTo(Poker p) {
		if(this.numPoker > p.numPoker)
			return 1;
		else if(this.numPoker == p.numPoker)
			return 0;
		else return -1;
	}
}
